package com.yidian.controller;

import com.yidian.dto.CommentDTO;
import com.yidian.entity.Comment;
import com.yidian.entity.Article;
import com.yidian.common.entity.Result;
import com.yidian.common.utils.RedisUtils;
import com.yidian.entity.User;
import com.yidian.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/comment")
@Api(value = "/comment" , tags = "评论接口")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 获取评论
     */
    @RequestMapping(value = "/allComments", method = RequestMethod.GET)
    @ApiOperation(value = "获取评论", notes = "获取所有评论", httpMethod = "GET")
    public Result<List<Comment>> getComments(Long commentId) {
        // 根据文章id获取文章所有评论
        List<Comment> AllComments = commentService.getComments(commentId);
        return Result.ok(AllComments);
    }

    /**
     * 获取病所有点赞数
     *
     * @return
     */
    @RequestMapping(value = "/addCommentLike", method = RequestMethod.POST)
    @ApiOperation(value = "获取评论点赞数", notes = "获取所有评论点赞数", httpMethod = "POST")
    public Result getLikings(Long commentId) {
        commentService.updateAddLikeCommentByCommentId(commentId);
        return Result.ok();
    }

    /**
     * 取消评论点赞
     */
    @RequestMapping(value = "/deleteCommentLike", method = RequestMethod.DELETE)
    @ApiOperation(value = "取消评论点赞", notes = "取消评论点赞", httpMethod = "DELETE")
    public Result deleteLikings(Long commentId) {
        // 取消点赞
        commentService.updateDeleteLikeCommentByCommentId(commentId);
        return Result.ok();
    }


    /**
     * 添加评论或评论回复
     */
    @RequestMapping(value = "/addComment", method = RequestMethod.POST)
    @ApiOperation(value = "添加评论或评论回复", notes = "添加评论或评论回复", httpMethod = "POST")
    public Result<List<CommentDTO>> addComments(int commentId) {
        // 添加评论
        List<CommentDTO> allComments = commentService.addComments(commentId,-1);
        return Result.ok(allComments);
    }
  /*  public void findChildren(CommentDTO parent, List<CommentDTO> fatherChildren) {

        // 找出直接子级
        List<CommentDTO> comments = parent.getChildren();

        // 遍历直接子级的子级
        for (CommentDTO comment : comments) {

            // 若非空，则还有子级，递归
            if (!comment.getChildren().isEmpty()) {
                findChildren(comment, fatherChildren);
            }

            // 已经到了最底层的嵌套关系，将该回复放入新建立的集合
            fatherChildren.add(comment);

            // 容易忽略的地方：将相对底层的子级放入新建立的集合之后
            // 则表示解除了嵌套关系，对应的其父级的子级应该设为空
            comment.setChildren(new ArrayList<>());
        }
    }
*/}